package com.e3.basdoc.measure.client;

import java.util.ArrayList;
import java.util.List;

import com.e3.basdoc.measure.model.MeasureUnitModel;
import com.e3.template.tgf.GFHelper;
import com.e3.web.widget.client.MyFormFieldFactory;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;

public class MeasureUnitHelper extends GFHelper<MeasureUnitModel>{
	
	public static MeasureUnitHelper INSTANCE = new MeasureUnitHelper();
	
	private String[] notNullFields = {MeasureUnitModel.CODE,MeasureUnitModel.NAME};
	
	public List<Field<?>> getFormFields(){
		String[] fNames = {MeasureUnitModel.ID,MeasureUnitModel.CODE,MeasureUnitModel.NAME};
		List<Field<?>> fields = new ArrayList<Field<?>>(fNames.length);
		for (String name:fNames) {
			Field<?> field = createFormField(name);
			fields.add(field);
		}
		return fields;
	}
	
	public Field<?> createFormField(String name){
		Field<?> formField = null;
		String dispName = getFieldDisplayName(name);
		boolean isNotNull = isNotNullField(name);
		if(name.equals(MeasureUnitModel.ID)){
			formField = MyFormFieldFactory.createIdField(name);
		}		
		else if(name.equals(MeasureUnitModel.CODE)){
			formField = MyFormFieldFactory.createStringField(name,dispName,10,isNotNull);
		}
		else if(name.equals(MeasureUnitModel.NAME)){
			formField = MyFormFieldFactory.createStringField(name,dispName,20,isNotNull);
			
		}
		return formField;
	}
	
	public List<ColumnConfig> getGridColumnConfigs(){
		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();
		RowNumberer numberer = new RowNumberer();   
		configs.add(numberer);
		
		ColumnConfig column = new ColumnConfig();
		String name = MeasureUnitModel.CODE;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);  
	    
	    column = new ColumnConfig();
		name = MeasureUnitModel.NAME;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);  
		    
	    return configs;
	}

	
	public String getFieldDisplayName(String code) {
		return MeasureUnitFieldsConstant.INSTANCE.getString(code);
	}

	@Override
	public String[] getNotNullFieldNames() {
		return notNullFields;
	}

	@Override
	public MeasureUnitModel createModelInstance() {
		return new MeasureUnitModel();
	}

}
